/**
 * @file trade/Home.js
 *  实例操作流水
 * @author
 */

import React, {Component, PropTypes} from 'react';
import {connect} from 'dva';
import {routerRedux, Link, withRouter} from 'dva/router';
import {
	Button,
	Modal,
	Row,
	Col,
	Table,
	Icon,
	Card,
	Form,
	message
} from 'antd';
import moment from 'moment';
import Tabtitle from '../../../components/common/TabTitle';
import Tablelist from '../../../components/common/TableData';
// import DateRangePicker  from '../component/searchBar/Datesearch';
import DateRangePicker from '../component/searchBar/DateSearchBar';
import Addtra from './Addtra';
import styles from './home.less';

const confirm = Modal.confirm;
const nowDate = moment(new Date()).format('YYYYMMDD');
const params = {
	table: 'iftrade',
	pagesize: 15,
	currentpage: 1
};
const Trade = ({
	dispatch,
	trade,
	location: {
		pathname,
		query
	}
}) => {
	if (trade.status) {
		const {tradelist,
			currentpage, 
			page, 
			state, 
			begindate, 
			enddate,
			msg,
			isRefresh,
			refreshList,} = trade;
		console.log('trade>>>>', trade,);
		const tag = "trade";
		const columns = [
			{
				title: '交易日期',
				dataIndex: 'tradeDay',
				key: 'tradeDay',
				sorter: true,
			}, {
				title: '基金号',
				dataIndex: 'fundId',
				key: 'fundId'
			}, {
				title: '组合号',
				dataIndex: 'combiNo',
				key: 'combiNo'
			}, {
				title: '实例号',
				dataIndex: 'instanceId',
				key: 'instanceId',
				sorter: true,
			}, {
				title: '证券名称',
				dataIndex: 'stockCode',
				key: 'stockCode',
				sorter: true,
			}, {
				title: '交易类型',
				dataIndex: 'tradeDirectionText',
				key: 'tradeDirectionText'
			}, {
				title: '平均基差',
				dataIndex: 'avgBasisSpread',
				key: 'avgBasisSpread'
			}, {
				title: '数量',
				dataIndex: 'dealAmount',
				key: 'dealAmount'
			}, {
				title: '汇总类型',
				dataIndex: 'tradeTypeText',
				key: 'tradeTypeText'
			}, {
				title: '备注',
				dataIndex: 'note',
				key: 'note'
			}, {
				title: '操作',
				key: 'action',
				render: item => (
					<span>
						<Link to={`/achievement/hedge/instance/revise/${item.tradeDay}-${item.fundId}-${item.combiNo}-${item.stockCode}-${encodeURIComponent(item.tradeDirectionText)}-${item.dealAmount}-${encodeURIComponent(item.tradeTypeText)}`}>编辑</Link>&nbsp;&nbsp;&nbsp;
						<a href="javascript:;" onClick={() => showConfirm(item)}>删除</a>
					</span>
				)
			}
		]
		const columns2 = [
			{
				title: '交易日期',
				dataIndex: 'tradeDay',
				key: 'tradeDay',
				sorter:(a, b) => a.tradeDay > b.tradeDay?1:-1,
			}, {
				title: '基金号',
				dataIndex: 'fundId',
				key: 'fundId'
			}, {
				title: '组合号',
				dataIndex: 'combiNo',
				key: 'combiNo'
			}, {
				title: '实例号',
				dataIndex: 'instanceId',
				key: 'instanceId',
				sorter: (a, b) => a.instanceId - b.instanceId,
			}, {
				title: '证券名称',
				dataIndex: 'stockCode',
				key: 'stockCode',
				sorter: (a, b) => a.stockCode - b.stockCode,
			}, {
				title: '交易类型',
				dataIndex: 'tradeDirectionText',
				key: 'tradeDirectionText'
			}, {
				title: '平均基差',
				dataIndex: 'avgBasisSpread',
				key: 'avgBasisSpread'
			}, {
				title: '数量',
				dataIndex: 'dealAmount',
				key: 'dealAmount'
			}, {
				title: '汇总类型',
				dataIndex: 'tradeTypeText',
				key: 'tradeTypeText'
			}, {
				title: '备注',
				dataIndex: 'note',
				key: 'note'
			}, 
		]
		// 删除
		function showConfirm(item) {
			console.log("item>>>", item);
			confirm({
				title: '您确定要删除此条数据?',
				onOk() {
					dispatch({
						type: 'trade/remove',
						payload: {
							...item,
							// table: 'iftrade',
							begindate: begindate,
							enddate: enddate
						}
					})
				},
				onCancel() {
					console.log('Cancel');
				}
			});
		}
		// 分页 pagesize
		function onShowSizeChange(current, pagesize) {
			console.log('query111',current, pagesize)
			console.log('onChangecurrentpage>>>',location,"query22>>",query)
			
				dispatch({
					type: 'trade/changeRouter',
					payload: {
						table: 'iftrade',
						pagesize:pagesize,
						currentpage:1,
						begindate: begindate,
						enddate: enddate,
						sortField: query.sortField,
						sortOrder: query.sortOrder,
					}
				})
		}
		function onChangecurrentpage(current,pagesize) {
			console.log('query111',current,pagesize)
			console.log('onChangecurrentpage>>>',location,"query11>>",query)
			// if(!_.isEmpty(query)){
				dispatch({
					type: 'trade/changeRouter',
					payload: {
						table: 'iftrade',
						currentpage: current,
						pagesize:pagesize,
						begindate: begindate,
						enddate: enddate,
						sortField: query.sortField,
						sortOrder: query.sortOrder,
					}
				})
			// }else{
			// 	dispatch({
			// 		type: 'trade/changeRouter',
			// 		payload: {
			// 			table: 'iftrade',
			// 			currentpage: current,
			// 			pagesize:pagesize,
			// 			begindate: begindate,
			// 			enddate: enddate
			// 		}
			// 	})
			// }
		}
		//排序
		function onSorterChange(pagination, filters, sorter) {
			console.log('location>>>',location,"query>>",query)
			const orderField = sorter.field;
			const orderSort = sorter.order;
			console.log(sorter, "orderField>>", orderField, "orderSort", orderSort);
			if(query.pagesize&&query.begindate&&query.enddate){
				dispatch({
					type: 'trade/changeRouter',
					payload: {
						table: 'iftrade',
						currentpage: currentpage,
						pagesize:query.pagesize,
						sortField: orderField,
						sortOrder: orderSort,
						begindate: begindate,
						enddate: enddate
					}
				})
			}else{
				dispatch({
					type: 'trade/changeRouter',
					payload: {
						table: 'iftrade',
						currentpage: currentpage,
						pagesize:15,
						begindate,
						enddate,
						sortField: orderField,
						sortOrder: orderSort,
					}
				})
			}
			
		}
		//导出
		function downLoad(){
			const nowBegindate = begindate || moment(new Date()).format('YYYYMMDD');
			const nowEnddate = enddate || moment(new Date()).format('YYYYMMDD');
			window.location.href=location.href.indexOf('localhost')>-1?`/api/ifdata/history/export?table=iftrade&begindate=${nowBegindate}&enddate=${nowEnddate}`:`/thfx/api/ifdata/history/export?table=iftrade&begindate=${nowBegindate}&enddate=${nowEnddate}`;
		}
		// 盈亏计算
		function tradeAccount() {
			dispatch({
				type:'trade/profitAndlossAccount',
				payload: {
					attributeDate:nowDate,
					local: 1
				}
			})
		}
		//刷新
		function onRefresh(){
			dispatch({
				type:'trade/onRefresh',
			})
			dispatch({
				type:'trade/setState',
				payload:{
					'isRefresh':1
				}
			})
		}
		function queryData() {
		console.log('beginDate>>>',begindate,'endDate>>>',enddate)
		if(begindate == "" || enddate == "" || begindate == "Invalid date"){
			message.warning('请选择时间');
			return;
		}else{
			dispatch({
				type: `${tag}/changeRouter`,
				payload: {
					table:`if${tag}`,
					pagesize:15,
					currentpage:1,
					begindate:begindate || BD,
					enddate:enddate || ED
				}
			})
			dispatch({
				type: `${tag}/setState`,
				payload: {
					'isRefresh':0
				}
			})
		}
	}
		return (
			<div>
				<Tabtitle value={'实例操作流水'}/>
				<div className="page-pdtmanagement-home content-inner">
					<Row className={styles.toolbar} type="flex" justify="start" align="middle">
						<Col span={4}>
							<DateRangePicker 
								tag={tag} 
								dispatch={dispatch} 
								begindate={begindate} 
								enddate={enddate} 
								location={location}
							/>
						</Col>
						{/*<Col span={2}>*/}
						<Col span={2} push={2}>
							<Button type="primary" onClick={queryData}>
			          查询
			        </Button>
						</Col>
						{/*<Col span={12} >*/}
						<Col span={12} push={2}>
							<Button type="primary" onClick={onRefresh}>实时刷新</Button>
						</Col>
						<Col span={2} >
							<Addtra  begindate={begindate} enddate={enddate}/>
						</Col>
						<Col span={2} >
							<Button type="primary" onClick={downLoad}>导出</Button>
						</Col>
						<Col span={2} >
							<Button type="primary" onClick={tradeAccount}>盈亏计算</Button>
						</Col>
					</Row>
						{
							isRefresh==0?<Tablelist 
								columns={columns} 
								data={tradelist} 
								page={page} 
								onShowSizeChange={onShowSizeChange} 
								onChangecurrentpage={onChangecurrentpage} 
								onSorterChange={onSorterChange} 
								record={record => `${record.tradeDay}-${record.fundId}-${record.combiNo}-${record.instanceId}-${record.tradeDirectionText}-${record.tradeTypeText}-${record.stockCode}-${record.dealAmount}-${record.avgBasisSpread}`
							}/>:<Table
									columns={columns2} 
									dataSource={refreshList}
									pagination={false}
									record={record => `${record.tradeDay}-${record.fundId}-${record.combiNo}-${record.instanceId}-${record.tradeDirectionText}-${record.tradeTypeText}-${record.stockCode}-${record.dealAmount}-${record.avgBasisSpread}`}
								/>

						}
					
				</div>
			</div>
		)
	} else {
		return null
	}
}

Trade.propTypes = {}
export default connect(({trade}) => ({trade}))(Trade)
